package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _purchaseOrderDetailService struct{}

func (s _purchaseOrderDetailService) CreateBatchApi(ctx context.Context, tx *dao.QueryTx, detail []*model.PurchaseOrdersDetailEntity) error {
	if err := tx.PurchaseOrdersDetailEntity.WithContext(ctx).CreateInBatches(detail, len(detail)); err != nil {
		return err
	}
	return nil
}

func (s _purchaseOrderDetailService) GetByPurchaseOrderIdApi(ctx context.Context, idList []int64) ([]*model.PurchaseOrdersDetailEntity, error) {
	return dao.PurchaseOrdersDetailEntity.WithContext(ctx).Where(dao.PurchaseOrdersDetailEntity.PurchaseOrdersID.In(idList...)).Find()
}

func (s _purchaseOrderDetailService) GetByPurchaseOrderDetailIdApi(ctx context.Context, id int64) (*model.PurchaseOrdersDetailEntity, error) {
	return dao.PurchaseOrdersDetailEntity.WithContext(ctx).Where(dao.PurchaseOrdersDetailEntity.ID.Eq(id)).First()
}

func (s _purchaseOrderDetailService) ModifyStatusByIdApi(ctx context.Context, tx *dao.QueryTx, id, status int64) error {
	if _, err := tx.PurchaseOrdersDetailEntity.WithContext(ctx).Where(dao.PurchaseOrdersDetailEntity.ID.Eq(id)).UpdateSimple(
		dao.PurchaseOrdersDetailEntity.StoreStatus.Value(status),
	); err != nil {
		return err
	}
	return nil
}

func (s _purchaseOrderDetailService) GetByPurchasePlanIdApi(ctx context.Context, purchasePlanId int64) (*model.PurchaseOrdersDetailEntity, error) {
	return dao.PurchaseOrdersDetailEntity.WithContext(ctx).Where(dao.PurchaseOrdersDetailEntity.PurchasePlanID.Eq(purchasePlanId)).First()
}

func (s _purchaseOrderDetailService) GetByPurchasePlanIdListApi(ctx context.Context, purchasePlanIdList []int64) ([]*model.PurchaseOrdersDetailEntity, error) {
	return dao.PurchaseOrdersDetailEntity.WithContext(ctx).Where(dao.PurchaseOrdersDetailEntity.PurchasePlanID.In(purchasePlanIdList...)).Find()
}

var PurchaseOrderDetailService = new(_purchaseOrderDetailService)
